﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using StackExchange.Redis;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Get_Shop_ListController : BaseController
    {
        public IShopService _shop { get; set; }
        [HttpGet]
        public IActionResult Get(int id, double longitude, double latitude)
        {
            longitude = 116.5484941006d;
            latitude = 31.7575372817d;
            string redisName = Request.Host.Host.ToLower().Replace(".", "_") + "_";
            GeoRadiusResult[] shops = ShopGeoHelper.GetGEO(longitude, latitude, 2000, 100, id,redisName,_shop);
            List<DTO.IndexShopModel> listshop = new List<DTO.IndexShopModel>();
            if (shops.Length > 0)
            {
                foreach (var item in shops)
                {
                    listshop.Add(new DTO.IndexShopModel { Distance = double.Parse(item.Distance.ToString()), Shop = _shop.GetModel(Convert.ToInt32(item.Member.ToString().Replace(redisName + "shopgeo_", ""))) });
                }
            }
            if (id > 0)
                listshop = listshop.Where(a => a.Shop.category_id == id).ToList();
            var sys = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            for (int i = 0; i < listshop.Count; i++)
            {
                listshop[i].Shop.img_url = sys.weburl + listshop[i].Shop.img_url;
            }
            return Ok(new { status = 1, list = listshop });
        }
    }
}